angular.module('BarcodeReplace')

.factory('BarcodeReplaceHomeService', ['$q', 'Http',
    function($q, Http) {
        
        //获取barcode信息
        function getBarcodeData(Barcode) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/CheckWork/getBarcodeData",
                data: {
                    Parameter: {
                        Barcode: Barcode,    
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }
        //芯片失效，取此芯片主档装配关系中最新的条码主档信息
        function getNewBarcodeData(initBarcodeMaster) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/BarcodeReplace/getNewBarcodeData",
                data: {
                    Parameter: {
                        initBarcodeMaster: initBarcodeMaster,    
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }

        //提交绑定
        //newBarcode新芯片
        //BarcodeMasterData(当前显示的)最新的条码信息
        //initBarcode初始Barcode(newBarcode和initBarcode进行绑定)

        function submitBarcodeData(BarcodeMasterData,newBarcode,initBarcodeMaster) {
            var deferred = $q.defer();
            Http.post({
                url: "/api/BarcodeReplace/submitBarcodeData",
                data: {
                    Parameter: {
                        BarcodeMasterData: BarcodeMasterData, 
                        newBarcode: newBarcode, 
                        initBarcodeMaster: initBarcodeMaster
                    }
                }
            }).then(function(data) {
                deferred.resolve(data);
            }, function(err) {
                deferred.reject(err);
            });
            return deferred.promise;
        }

        
        return {
            getBarcodeData: getBarcodeData,
            getNewBarcodeData: getNewBarcodeData,
            submitBarcodeData: submitBarcodeData
        };

    }
]);